// pages/cardSquare.js
let app = getApp();
let util = require("../../utils/util");
let common = require("../../common/common");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    industryList: common.industryList,
    index: 0,
    address: ["", "", ""], // 地址

    list: [], // 搜索结果列表
    isListNone: false, // 搜索结果为空

    listNoneImg: "../../image/listNone.png"
  },

  bindIndustryPickerChange: function (e) {
    console.log(" in cardSquare industry e: ", e);
    let industry = common.industryList[e.detail.value];
    let that = this;

    let payload = {};
    payload.industry = industry;

    wx.request({
      url: common.domain + common.prefixUrl,
      method: "GET",
      data: {
        cond: payload,
      },
      success: res => {
        console.log(res);
        let list = res.data.data.list;
        that.setData({
          list: list,
        });

        if (list.length === 0) {
          that.setData({
            isListNone: true,
          });
        } else {
          that.setData({
            isListNone: false,
          });
        }
      }
    });
  },

  bindAddressPickerChange: function (e) {
    console.log(" in cardSquare address e: ", e);
    let address = e.detail.value;
    let that = this;

    let payload = {};
    payload["address.province"] = address[0];
    payload["address.city"] = address[1];
    payload["address.area"] = address[2];

    wx.request({
      url: common.domain + common.prefixUrl,
      method: "GET",
      data: {
        cond: payload,
      },
      success: res => {
        console.log(res);
        let list = res.data.data.list;
        that.setData({
          list: list,
        });

        if (list.length === 0) {
          that.setData({
            isListNone: true
          });
        } else {
          that.setData({
            isListNone: false,
          });
        }
      }
    });
  },

  searchChange: function (e) {
    console.log(" in cardSquare searchChange e: ", e);

    let that = this;
    let value = e.detail.value;
    let condition1 = {
      name: {
        $regex: value,
        $options: "im",
      }
    };
    let condition2 = {
      company: {
        $regex: value,
        $options: "im",
      }
    };
    let payload = { "$or": [condition1, condition2] };

    wx.request({
      url: common.domain + common.prefixUrl,
      method: "GET",
      data: {
        cond: JSON.stringify(payload),
      },
      success: res => {
        let list = res.data.data.list;
        that.setData({
          list: list,
        });

        if (list.length === 0) {
          that.setData({
            isListNone: true
          });
        } else {
          that.setData({
            isListNone: false,
          });
        }
        
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})